<template>
  <BaseModal :title="modalTitle" :visible="visible" @cancel="handleCancel" @ok="handleOk" width="800px" destroyOnClose>
    <custom-form
      ref="ruleForm"
      :isValidate="true"
      :editModel="''"
      :rules="rules"
      :form="formInfo"
      :formList="formItemList"
      @selectNode="handleSelectOrg"
    ></custom-form>
  </BaseModal>
</template>

<script>
import BaseModal from '@/components/BaseModal'
import YouthTreeSelect from '../../componments/youthTreeSelect.vue'
import { packageConfirm } from '@/utils/baseAction.js'
import { inwardTransfer, transferOut } from '@/api/partyMasses/leagueManage'

export default {
  name:'a'+ Date.now(),
  components: {
    BaseModal,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    modalType: {
      type: String,
      default: 'turnIn'
    },
    initData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      formInfo: {},
      rules: {
        youthOrgId: [{ required: true, message: '请选择', trigger: 'change' }],
      },
    }
  },
  computed: {
    modalTitle() {
      const map = {
        turnIn: '对内转出',
        turnOut: '对外转出'
      }
      return map[this.modalType]
    },
    formItemList() {
      return this.modalType == 'turnIn' ?
      [
        {
          label: '团员名称',
          keyName: 'youthMemberName',
          disabled: true,
          type: 'BaseInput',
          span: 12,
        },
        {
          label: '转入团支部',
          keyName: 'youthOrgId',
          type: YouthTreeSelect,
          span: 12,
          attrs: {
            onlySelectTypes: [ 3 ]
          }
        },
        {
          label: '备注说明',
          keyName: 'remarks',
          type: 'BaseTextarea',
          span: 24,
        },
      ] : [
        {
          label: '团员名称',
          keyName: 'youthMemberName',
          disabled: true,
          type: 'BaseInput',
          span: 12,
        },
        {
          label: '备注说明',
          keyName: 'remarks',
          type: 'BaseTextarea',
          span: 24,
        }
      ]
    },
  },
  watch: {
    initData: {
      immediate: true,
      deep: true,
      handler(val) {
        this.formInfo = this.modalType == 'turnIn' ? ({ ...val, youthOrgId: void 0 } || {}) : ({ ...val } || {})
      },
    },
  },
  methods: {
    handleSelectOrg(key, data) {
      const { id } = data
      this.formInfo = {
        ...this.formInfo,
        youthOrgId: id,
      }
      this.$nextTick(() => {
        this.$refs.ruleForm.validateField('youthOrgId')
      })
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          this.$message.warning("请填写完所有必填项信息")
          return
        }
        packageConfirm({
          content: `请确认是否进行${this.modalTitle}？`,
          callback: async () => {
            const params = {
              ...this.formInfo,
            }
            delete params.partyMemberName
            const api = this.modalType == 'turnIn' ? inwardTransfer : transferOut;
            const res = await api(params)
            if (res && res.code == 0) {
              this.$message.success(`${this.modalTitle}成功`)
              this.handleCancel();
              this.$emit('ok')
            } else {
              this.$message.error(`${this.modalTitle}失败！`);
            }
          },
        })
      })
    },
    handleCancel() {
      this.$emit('update:visible')
    },
  },
}
</script>

<style>
</style>
